#!/bin/bash

# if this script isn't already running
if [[ ! "`pidof -x $(basename $0) -o %PPID`" ]]; then

    . /usr/sbin/so-common

{%- from 'vars/globals.map.jinja' import GLOBALS %}
{%- from 'idstools/map.jinja' import IDSTOOLSMERGED %}

{%- set proxy = salt['pillar.get']('manager:proxy') %}
{%- set noproxy = salt['pillar.get']('manager:no_proxy', '') %}

{%- if proxy %}
# Download the rules from the internet
    export http_proxy={{ proxy }}
    export https_proxy={{ proxy }}
    export no_proxy="{{ noproxy }}"
{%- endif %}

    mkdir -p /nsm/rules/suricata
    chown -R socore:socore /nsm/rules/suricata
{%- if not GLOBALS.airgap %}
# Download the rules from the internet
{%-   if IDSTOOLSMERGED.config.ruleset == 'ETOPEN' %}
    docker exec so-idstools idstools-rulecat -v --suricata-version 7.0.3 -o /nsm/rules/suricata/ --merged=/nsm/rules/suricata/emerging-all.rules --force
{%-   elif IDSTOOLSMERGED.config.ruleset == 'ETPRO' %}
    docker exec so-idstools idstools-rulecat -v --suricata-version 7.0.3 -o /nsm/rules/suricata/ --merged=/nsm/rules/suricata/emerging-all.rules --force --etpro={{ IDSTOOLSMERGED.config.oinkcode }}
{%-   endif %}
{%- endif %}


    argstr=""
    for arg in "$@"; do
        argstr="${argstr} \"${arg}\""
    done

    docker exec so-idstools /bin/bash -c "cd /opt/so/idstools/etc && idstools-rulecat --force ${argstr}"

fi
